package br.boicotando.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.boicotando.persistencia.ECarrinho;

public class CarrinhoDaoDB extends DaoDB<ECarrinho> implements CarrinhoDao{

	public CarrinhoDaoDB(Session sess) {
		super(sess);
	}

	@Override
	@SuppressWarnings("unchecked")
	public ECarrinho consultarId(String id) {
		Query qry = sess.createQuery("select c from ECarrinho c where c.id = :id");
		qry.setParameter("id", id);
		List<ECarrinho> carrinhos = (List<ECarrinho>) qry.list();
		if (!carrinhos.isEmpty()) {
			return (ECarrinho) carrinhos.get(0);
		}		
		return null;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ECarrinho> consultarTodos() {
		Query qry = sess.createQuery("select o from ECarrinho o");
		List<ECarrinho> carrinhos = (List<ECarrinho>) qry.list();
		return carrinhos;
	}

	@Override
	@SuppressWarnings("unchecked")
	public ECarrinho consultaCarrinhoAberto(String idCliente) {
		Query qry = sess.createQuery("select o from ECarrinho o join o.cliente c"
				                    +" where o.pedido is null and c.id = :id_cliente");
		qry.setParameter("id_cliente", idCliente);
		List<ECarrinho> carrinhos = (List<ECarrinho>) qry.list();
		if (!carrinhos.isEmpty()) {
			return (ECarrinho) carrinhos.get(0);
		}		
		return null;
	}

}
